package com.meta.nd.factory;

import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import org.springframework.util.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class NvFactory {

public static Nv createNv(Nd nd){
    String nvClassName = nd.getNvClassName();
    if (StringUtils.isEmpty(nvClassName))
        return new Nv(nd);
    else
        return createNv(nvClassName,nd);
}

    public static Nv createNv(String nvClassName, Nd nd){
        Class clazz;
        try {
            clazz = Class.forName(nvClassName);
        } catch (ClassNotFoundException e) {
            String msg = String.format("Nv的类名不不存在:%s", nvClassName);
            throw new RuntimeException(msg,e);
        }
        try {
            Constructor<?> constructor = clazz.getConstructor(Nd.class);
            Nv nv = (Nv)constructor.newInstance(nd);
            return nv;
        } catch (InstantiationException e) {
            String msg = String.format("创建类实例失败，类名:%s", nvClassName);
            throw new RuntimeException(msg,e);
        } catch (IllegalAccessException e) {
            String msg = String.format("创建类实例失败，类名:%s", nvClassName);
            throw new RuntimeException(msg,e);
        } catch (NoSuchMethodException e) {
            String msg = String.format("创建类实例失败，类名:%s", nvClassName);
            throw new RuntimeException(msg,e);
        } catch (InvocationTargetException e) {
            String msg = String.format("创建类实例失败，类名:%s", nvClassName);
            throw new RuntimeException(msg,e);
        }
    }

}
